<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true">
        <el-select v-model="selectOptions.query" placeholder="请选择">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
        <el-form-item>
          <el-input :placeholder="selectOptions.query" v-model="selectOptions.keywords" @keyup.enter.native="doSearch"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="doSearch">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" style="background: #13ce66" v-on:click="_reload">刷新</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table :data="orders" highlight-current-row v-loading="listLoading"
              style="width: 100%;" element-loading-text="玩命请求中。。。">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column label="类型" width="80" prop="buy_type" :formatter="buyType">
      </el-table-column>
      <el-table-column prop="nickname" label="用户名" width="160">
      </el-table-column>
      <el-table-column prop="phone" label="手机号" width="140">
      </el-table-column>
      <el-table-column prop="amount" label="订单金额(元)" width="120" :formatter="orderAmount">
      </el-table-column>
      <el-table-column prop="pay_type" label="支付方式" width="150" :formatter="payType">
      </el-table-column>
      <el-table-column prop="buy_goods_key" label="子订单号" width="100">
      </el-table-column>
      <el-table-column label="支付单号" min-width="200" prop="id">
      </el-table-column>
      <el-table-column label="流水号" prop="transaction_id" min-width="200">
      </el-table-column>
      <el-table-column prop="status" label="订单状态" width="100" :formatter="orderStatus">
      </el-table-column>
      <el-table-column label="创建时间" prop="create_time" :formatter="createTime" width="180">
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-pagination
        @current-change="currentPageChange"
        :page-size="30"
        layout="prev, pager, next, jumper"
        :total="total" style="float:right;" :current-page="parseInt(selectOptions.page)">
      </el-pagination>
      <span style="float: right;margin-top: 6px;margin-right: 5px">共 {{total}} 条数据</span>
    </el-col>

  </section>
</template>

<script type="text/ecmascript-6">
  import util from '../../util'
  import NProgress from 'nprogress'
	import { apiTree } from '../../apiTree'

  export default {
    data() {
      return {
        options: [
          {value: 'id', label: '支付单号'},
          {value: 'phone', label: '手机号'},
          {value: 'nickname', label: '昵称'},
          {value: 'transaction_id', label: '流水号'}
        ],
        orders: [],
        total: 0,
        listLoading: false,
        selectOptions: {
          page: this.$route.query.page !== undefined ? this.$route.query.page : 1,
          query: this.$route.query.query !== undefined ? this.$route.query.query : '',
          keywords: this.$route.query.keywords !== undefined ? this.$route.query.keywords : ''
        }
      }
    },
    methods: {
      // 购买类型
      buyType(row, column) {
        return row.buy_type === '1' ? '购地' : '其它'
      },
      // 支付方式
      payType(row, column) {
        if (row.pay_type === '2') {
          return '支付宝'
        } else if (row.pay_type === '3') {
          return '微信'
        } else {
          return '其它'
        }
      },
      orderStatus(row, column) {
        return row.status === '0' ? '待支付' : '成功'
      },
      // 订单金额
      orderAmount(row, column) {
        return parseInt(row.amount) / 100
      },
      // 创建时间
      createTime(row, column) {
        return util.formatDate.format(new Date(row.create_time * 1000), 'yyyy/MM/dd hh:mm:ss')
      },
      currentPageChange(val) {
        this.selectOptions.page = val;
        this.$router.push({path: this.$route.path, query: this.selectOptions});
      },
      // 搜索
      doSearch() {
        if (this.selectOptions.query) {
          this.$router.push({path: this.$route.path, query: this.selectOptions});
        }
      },
      _reload() {
        this.getOrder()
      },
      // 获取用户列表
      getOrder() {
        let para = this.$route.query
        if (util.isEmptyObject(para)) {
          this.selectOptions = {
            page: 1,
            query: '',
            keywords: ''
          }
        }
        this.listLoading = true
        NProgress.start()
        apiTree('order/index', 'order/index',para).then((res) => {
          this.total = res.d.total_num
          this.orders = res.d.data
          this.listLoading = false
          NProgress.done()
        })
      }
    },
    created() {
      this.getOrder()
    },
    mounted() {
//      console.log(this.selectOptions)
    },
    watch: {
      $route: 'getOrder'
    }
  }

</script>

<style scoped lang="stylus" rel="stylesheet/stylus">
  .user-avatar
    width 60px
    height 60px
    border-radius 50%
    & img
      width 100%
      height 100%
      border-radius 50%
</style>
